﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.AdminIstrator
{
    public partial class DoanhThu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    txtBeginDate.Text = "" + Request.QueryString["beginDate"];
                    txtEndDate.Text = "" + Request.QueryString["endDate"];
                    BindListDelivery();
                }
                else
                {
                    Response.Redirect("/AdminIstrator/Login.aspx");
                }
            }
        }

        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }

        private void BindListDelivery()
        {
            List<Data.DeliveryInfo> listDelivery = new List<Data.DeliveryInfo>();
            if (txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=3", "DeliveryApprovedDate desc");
                lblDate.Text = "TỔNG DOANH THU";
                
            }
            else if (txtBeginDate.Text.Equals("") && !txtEndDate.Text.Equals(""))
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=3 and DateDiff(day,DeliveryDate,'" + txtEndDate.Text + "')>=0", "DeliveryApprovedDate desc");
                lblDate.Text = "DOANH THU ĐẾN NGÀY: "+txtEndDate.Text;
            }
            else if (!txtBeginDate.Text.Equals("") && txtEndDate.Text.Equals(""))
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=3 and DateDiff(day,'" + txtBeginDate.Text + "',DeliveryDate)>=0", "DeliveryApprovedDate desc");
                lblDate.Text = "DOANH THU TỪ NGÀY: "+txtBeginDate.Text;
            }
            else
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=3 and DateDiff(day,'" + txtBeginDate.Text + "',DeliveryDate)>=0 and DateDiff(day,DeliveryDate,'" + txtEndDate.Text + "')>=0", "DeliveryApprovedDate desc");
                lblDate.Text = "DOANH THU TỪ NGÀY " + txtBeginDate.Text + " ĐẾN NGÀY " + txtEndDate.Text;
            }
            rpDelivery.DataSource = listDelivery;
            rpDelivery.DataBind();
            int sum = 0;
            for (int i = 0; i < rpDelivery.Items.Count; i++)
            {

                Label lblPrice = (Label)rpDelivery.Items[i].FindControl("lblPrice");
                if (lblPrice.Text == "")
                    lblPrice.Text = "0 ";
                string temp = lblPrice.Text.Split('.')[0];
                lblPrice.Text = Format_Price(lblPrice.Text.Replace(".0000",""));
                if (temp != "")
                {
                    sum += Convert.ToInt32(temp);
                }

            }
            Label lblSumPrice = (Label)rpDelivery.Controls[0].Controls[0].FindControl("lblSumPrice");
           
            lblSumPrice.Text = Format_Price(sum.ToString()) + " VND";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindListDelivery();
            Response.Redirect("/AdminIstrator/DoanhThu.aspx?beginDate=" + txtBeginDate.Text + "&endDate=" + txtEndDate.Text);
        }

        protected void lbtPrint_Click(object sender, EventArgs e)
        {
            Response.Redirect("/AdminIstrator/DoanhThuPrint.aspx?tungay=" + Request.QueryString["beginDate"] + "&denngay=" + Request.QueryString["endDate"]);
        }

       
    }
}